package pl.jtb.ctl.tabory.dao.impl.mybatis.base;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

public class BaseDaoService<T> {

    private final Logger logger = Logger.getLogger(BaseDaoService.class);

    protected SqlSessionFactory sessionFactory;

    public BaseDaoService(SqlSessionFactory sqlSessionFactory) {
        this.sessionFactory = sqlSessionFactory;
    }

    @SuppressWarnings("unchecked")
    protected T selectOne(final String statement, final Object object) throws Exception {
        logger.debug("Select one for statement: " + statement + ", object:" + object.toString());
        SqlSession session = sessionFactory.openSession();
        T result = null;
        try {
            result = (T) session.selectOne(statement, object);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            session.rollback();
            throw e;
        } finally {
            session.close();
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    protected List<T> selectList(final String statement, final Object object) throws Exception {
        logger.debug("Select list for statement: " + statement);
        SqlSession session = sessionFactory.openSession();
        List<T> result = null;
        try {
            result = session.selectList(statement, object);
            session.commit();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            session.rollback();
            throw e;
        } finally {
            session.close();
        }
        return result;
    }

    protected void create(final String statement, final Object object) throws Exception {
        logger.debug("Save for statement: " + statement);
        SqlSession session = sessionFactory.openSession();
        try {
            session.insert(statement, object);
            session.commit();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            session.rollback();
            throw e;
        } finally {
            session.close();
        }
    }

    protected void update(final String statement, final Object object) throws Exception {
        logger.debug("Update for statement: " + statement);
        SqlSession session = sessionFactory.openSession();
        try {
            session.update(statement, object);
            session.commit();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            session.rollback();
            throw e;
        } finally {
            session.close();
        }
    }

    protected void delete(final String statement, final Object object) throws Exception {
        logger.debug("Delete for statement: " + statement);
        SqlSession session = sessionFactory.openSession();
        try {
            session.delete(statement, object);
            session.commit();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            session.rollback();
            throw e;
        } finally {
            session.close();
        }
    }

}